package com.yjzx.util.common.util.file;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.file.Files;

/**
 * @author yjzx
 * @date 2024/1/4
 * @description FileUtils
 */
@Slf4j
public class FileUtils {
    /**
     * 读取文件为byte
     *
     * @param file
     * @return
     */
    public static byte[] readFile(File file) {
        if (file != null && file.isFile()) {
            try (FileInputStream fis = new FileInputStream(file)) {
                return readFully(fis);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return new byte[0];
    }

    /**
     * 读取流文件返回字节数组
     *
     * @param inputStream 流
     * @return 字节数组
     * @throws IOException
     */
    public static byte[] readFully(InputStream inputStream) throws IOException {
        if (inputStream != null) {
            byte[] buffer = new byte[0X400];
            int bytesRead;
            try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    baos.write(buffer, 0, bytesRead);
                }
                return baos.toByteArray();
            }
        }else {
            return new byte[0];
        }

    }

    public static void writeOutputStream(OutputStream outputStream, byte[] data) {
        if (outputStream != null&& data != null) {
            try {
                outputStream.write(data);
                outputStream.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 将指定byte写输入文件
     *
     * @param file
     * @param data
     */
    public static void writeFile(File file, byte[] data) {
        if (file == null|| data == null) {
            return;
        }
        // 获取文件的父目录
        File parentDir = file.getParentFile();

        try {
            // 检查并创建父目录
            if (parentDir != null ) {
                parentDir.mkdirs();
            }
            if (!file.exists()) {
                file.createNewFile();
            }
            try (FileOutputStream fos = new FileOutputStream(file)){
                writeOutputStream(fos, data);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 指定流文件复制到outputStream位置
     *
     * @param inputStream
     * @param outputStream
     */
    public static void copy(InputStream inputStream, OutputStream outputStream) {
        if (inputStream instanceof FileInputStream && outputStream instanceof FileOutputStream) {
            try (FileChannel sourceChannel = ((FileInputStream) inputStream).getChannel();
                 FileChannel destChannel = ((FileOutputStream) outputStream).getChannel()) {
                destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            try {
                byte[] buf = new byte[1024];
                int bytesRead;
                while ((bytesRead = inputStream.read(buf)) > 0) {
                    outputStream.write(buf, 0, bytesRead);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    /**
     * 复制文件
     *
     * @param source 原文件
     * @param dest   新文件
     */
    public static void copyFileUsingFileChannels(File source, File dest) {
        try (FileInputStream inputStream = new FileInputStream(source);
             FileOutputStream outputStream = new FileOutputStream(dest)) {
            copy(inputStream, outputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 递归删除目录及其所有内容
     *
     * @param directory 要删除的目录
     * @return 如果删除成功，返回 true；否则返回 false
     */
    public static boolean deleteDirectory(File directory) {
        if (directory == null || !directory.exists()||!directory.isDirectory()) {
            return false;
        }
        File[] files = directory.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isDirectory()) {
                    // 递归删除子目录
                    if (!deleteDirectory(file)) {
                        return false;
                    }
                } else {
                    // 删除文件
                    if (!file.delete()) {
                        return false;
                    }
                }
            }
        }
        return directory.delete();
    }

    /**
     * 删除文件
     *
     * @param file
     */
    public static boolean deleteFile(File file) {
        if (file != null&&file.isFile()){
            return file.delete();
        }
        return false;
    }


}
